#include <iostream>
#include <string>
using namespace  std;


// modidy 20210722
//define the const  top of contact
#define MAX 1000

// contact struct
struct Person {
    string m_fullName;
    int m_Sex;
    int m_Age;
    string m_Phone;
    string m_Addr;

};

//UIMS struct
struct Addressbooks {
    struct Person personArray[MAX];
    int m_Size;

};


// menu list
void showMenu() {
    cout << "***********************************" <<endl;
    cout << "********  1. Add a Contact  *******" <<endl;
    cout << "********  2. Show Contacts  *******" <<endl;
    cout << "********  3. Delete a Contact  ****" <<endl;
    cout << "********  4. Search a Contact  ****" <<endl;
    cout << "********  5. Edit a Contact  ******" <<endl;
    cout << "********  6. Clear Contacts  ******" <<endl;
    cout << "********  0. Exit UIMS      *******" <<endl;
    cout << "***********************************" <<endl;


}
void addPerson (Addressbooks * abc) {
    //Determine if the address book is full
    if (abc->m_Size == MAX) {
        cout << "Addressbook is full" << endl;
        return;
    } else {
        string fullname;
        cout << "Plese type your fullname:" << endl;
        cin >> fullname;
        abc->personArray[abc->m_Size].m_fullName = fullname;

        cout << "Please type your gender:" << endl;
        cout << "1 -- Male" << endl;
        cout << "2 -- Female" << endl;

        int sex = 0;
        while (true) {
            cin >> sex;
            if (sex == 1 || sex == 2) {
                abc->personArray[abc->m_Size].m_Sex = sex;
                break;
            }
            cout << "type error" << endl;
        }

        //age
        cout << "Please type your age:" << endl;
        int age = 0;
        cin >> age;
        abc->personArray[abc->m_Size].m_Age = age;

        //address
        cout << "Please type your address:"<<endl;
        string address;
        cin >> address;
        abc->personArray[abc->m_Size].m_Addr = address;

        //家庭住址
        cout <<"Please type your phone:"<<endl;
        string phone = "";
        cin >> phone;
        abc->personArray[abc->m_Size].m_Phone = phone;

        //update count for addressbooks
        abc->m_Size++;

        cout <<"Add Success"<<endl;
    }
}

void showPerson(Addressbooks * abc){
    if (abc->m_Size == 0){
        cout <<"Contact is empty"<<endl;
    } else{
        for (int i = 0; i < abc->m_Size; i++){
            cout<<"FullName:"<< abc->personArray[i].m_fullName <<"\t";
            cout<<"Age:"<< abc->personArray[i].m_Age <<"\t";
            cout<<"Sex:"<< (abc->personArray[i].m_Sex == 1 ? "Man":"Woman") <<"\t";
            cout<<"Address:"<< abc->personArray[i].m_Addr <<"\t";
            cout<<"Phone:"<< abc->personArray[i].m_Phone <<"\t\n";
        }
    }
}
//make sure your search name is in the contact
int isExist (Addressbooks * abc, string fullname){
    for (int i = 0; i < abc->m_Size; i++){
        if (abc->personArray[i].m_fullName == fullname){
            return i;
        }
    }
    return -1;
}

//delete person function
void delPerson (Addressbooks * abc){
    cout <<"Please type delete person:" <<endl;
    string fullname;
    cin >> fullname;

    int ret = isExist(abc, fullname);
    if (ret != -1){
        for (int i = ret; i<abc->m_Size; i++){
            abc->personArray[i] = abc->personArray[i+1];
        }
        abc->m_Size--;
        cout << "Delete Success" <<endl;
    } else{
        cout << "There is no such person："<< fullname<<endl;
    }
}

//search person
void searchPerson(Addressbooks * abc){
    cout<<"Pleae type you want search person:" <<endl;
    string fullname;
    cin >> fullname;
    int ret = isExist(abc, fullname);
    if (ret != -1){
        cout<<"FullName:"<< abc->personArray[ret].m_fullName <<"\t";
        cout<<"Age:"<< abc->personArray[ret].m_Age <<"\t";
        cout<<"Sex:"<< abc->personArray[ret].m_Sex <<"\t";
        cout<<"Address:"<< abc->personArray[ret].m_Addr <<"\t";
        cout<<"Phone:"<< abc->personArray[ret].m_Phone <<"\t\n";
    } else {
        cout <<"There is no such person:"<<fullname<<endl;
    }
}


// edit person
void editPerson(Addressbooks * abc){
    cout << "Pleae type your edit person:"<<endl;
    string fullname;
    cin >> fullname;

    int ret = isExist(abc, fullname);
    if (ret != -1){
        string editfullname;
        cout << "Plese type your fullname:" << endl;
        cin >> editfullname;
        abc->personArray[ret].m_fullName = editfullname;

        cout << "Please type your gender:" << endl;
        cout << "1 -- Male" << endl;
        cout << "2 -- Female" << endl;

        int sex = 0;
        while (true) {
            cin >> sex;
            if (sex == 1 || sex == 2) {
                abc->personArray[ret].m_Sex = sex;
                break;
            }
            cout << "type error" << endl;
        }

        //age
        cout << "Please type your age:" << endl;
        int age = 0;
        cin >> age;
        abc->personArray[ret].m_Age = age;

        //address
        cout << "Please type your address:"<<endl;
        string address;
        cin >> address;
        abc->personArray[ret].m_Addr = address;

        //家庭住址
        cout <<"Please type your phone:"<<endl;
        string phone = "";
        cin >> phone;
        abc->personArray[ret].m_Phone = phone;

        cout <<"Edit Success"<<endl;
    }
}

//clear contact
void clearPerson(Addressbooks * abc){
    abc->m_Size = 0;
    cout <<"Contact cleared"<<endl;
}

int main() {
    // Create Contacts
    Addressbooks abc;

    //init count
    abc.m_Size = 0;

    int select = 0;
    while(true){
        showMenu();
        cin >> select;
        switch (select) {
            case 1:
                addPerson(&abc);
                break;
            case 2:
                showPerson(&abc);
                break;
            case 3:
                delPerson(&abc);
                break;
            case 4:
                searchPerson(&abc);
                break;
            case 5:
                editPerson(&abc);
                break;
            case 6:
                clearPerson(&abc);
                break;
            case 0:
                cout <<"Welcome to you next time"<<endl;

                //freeze screem in Windows
                //system("pause")
                return 0;
                break;

            default:
                cout << "type error" << endl;
                break;
        }
    }
}